<?php

namespace App\Admin\Controllers\Dedusting;

use App\Models\DedustingLibrary;
use App\Http\Controllers\Controller;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Callout;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Form;
use Dcat\Admin\Admin;
use Dcat\Admin\Show;
use Illuminate\Http\Request;
use App\Handlers\IotCardHandler;

class LibraryController extends Controller
{
    public function index(Content $content)
    {
        return $content
            ->header('文库列表')
            ->description('')
            ->body($this->grid());
    }

    protected function grid()
    {
        return new Grid(new DedustingLibrary(), function (Grid $grid) {
            $grid->column('title', '标题');
            $grid->column('type', '类型')->using(DedustingLibrary::TYPE_TEXT);
            $grid->column('created_at', '创建时间');
            $grid->column('updated_at', '修改时间');
        });
    }

    public function show($id, Content $content)
    {
        return $content->header('文库')
            ->description('详情')
            ->body(Show::make($id, new DedustingLibrary(), function (Show $show) {
                $show->field('title', '标题');
                $show->field('type', '类型')->using(DedustingLibrary::TYPE_TEXT);
                $show->field('question', '问题现象');
                $show->field('question_img', '问题现象图片')->image();
                $show->field('question_video', '问题现象视频')->video();  //自己加的，改基库
                
                $show->field('judge', '判断说明');
                $show->field('analyse', '原因分析');
                $show->field('answer', '应对措施');
                $show->field('answer_img', '应对措施图片')->image();
                $show->field('answer_video', '应对措施视频')->video();

                $show->field('created_at', '创建时间');
                $show->field('updated_at', '修改时间');
            }));
    }

    public function update($id){
        return $this->form()->update($id);
    }
    public function store(){
        return $this->form()->store();
    }

    public function create(Content $content){
        return $content
            ->title('新建文库')
            ->description('创建')
            ->body($this->form());
    }

    public function form(){
        return Form::make(new DedustingLibrary(), function (Form $form) {
            $form->text('title', '标题')->required();
            $form->image('cover', '封面')->move(DedustingLibrary::COVER_PATH)->uniqueName()->saveFullUrl()->autoUpload()->required();
            $form->radio('type', '类型')->options(DedustingLibrary::TYPE_TEXT)->default(DedustingLibrary::TYPE_1);
            $form->textarea('question', '问题现象')->required();
            $form->multipleImage('question_img', '问题现象图片')
                ->move(DedustingLibrary::QUESTION_IMAGE_PATH)
                ->uniqueName()
                ->saveFullUrl()
                ->saving(function ($paths) {
                    return json_encode($paths, JSON_UNESCAPED_SLASHES);
                })
                ->autoUpload()
                ->required();
            $form->multipleFile('question_video', '问题现象视频')
                ->move(DedustingLibrary::QUESTION_VIDEO_PATH)
                ->uniqueName()
                ->saveFullUrl()
                ->saving(function ($paths) {
                    return json_encode($paths, JSON_UNESCAPED_SLASHES);;
                })
                ->autoUpload()
                ->required();
            $form->textarea('judge', '判断说明')->required();
            $form->textarea('analyse', '原因分析')->required();
            $form->textarea('answer', '应对措施')->required();
            $form->multipleImage('answer_img', '应对措施图片')
                ->move(DedustingLibrary::ANSWER_IMAGE_PATH)
                ->uniqueName()
                ->saveFullUrl()
                ->saving(function ($paths) {
                    return json_encode($paths, JSON_UNESCAPED_SLASHES);
                })
                ->autoUpload()
                ->required();
            $form->multipleFile('answer_video', '应对措施视频')
                ->move(DedustingLibrary::ANSWER_VIDEO_PATH)
                ->uniqueName()
                ->saveFullUrl()
                ->saving(function ($paths) {
                    return json_encode($paths, JSON_UNESCAPED_SLASHES);
                })
                ->autoUpload()
                ->required();

            $form->hidden('user_id')->value(Admin::user()->id);

            $form->saving(function (Form $form) {
                $form->user_id = Admin::user()->id;
            });
        });
    }

    public function edit($id,Content $content){
        return $content
            ->title('文库编辑')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    public function destroy($id)
    {
        return $this->form()->destroy($id);
    }

    public function libraries(Request $request)
    {
        $q = $request->get('q');

        if (empty($q)) {
            return DedustingLibrary::where('id', '>', 0)->get(['id', DB::raw('title as text')]);
        } else {
            return DedustingLibrary::where('title', 'like', "%$q%")->get(['id', DB::raw('title as text')]);
        }
    }
}